<?php
require_once('db_fns.php');

function get_user_urls($username) {
	//extract from the database all the urls this user has stored
	$con = db_connect();
	$result = $con->query("select bm_URL from bookmark where username = '".$username."'");

	if (!$result) {
		return false;
	}

	//create an array of the URLs
	$url_array = array();
	for ($count = 1; $row = $result->fetch_row(); ++$count) {
		$url_array[$count] = $row[0];
	}
	return $url_array;
	//以数组的形式获取书签，返回这个数组
	//
}

function add_bm($new_url) {
	//add new  bookmark to the database

	echo "Attempting to add ". htmlspecialchars($new_url)."<br />";
	$valid_user = $_SESSION['valid_user'];

	$con = db_connect();

	//check not a repeat bookmark
	$result = $con->query("select * from bookmark where username='".$username."' and bm_URL='".$new_url."'");

	if ($result && ($result->num_rows>0)) {
		throw new Exception("bookmark already exists.");
		
	}

	//insert the new bookmark
	if (!$con->query("insert into bookmark values('".$valid_user."', '".$new_url."')"))	 {
		throw new Exception("Bookmark could not be inserted.");
		
	} return true;
}